package com.archtecture.view.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.archtecture.control.models.ModelAb;

@FacesConverter("ModelAbConverter")
public class ModelAbConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {

		try {

			String[] t = arg2.split(":");
			ModelAb ab = (ModelAb) Class.forName(t[0].replace("class ", "")).newInstance();
			ab.setCodigo(Integer.valueOf(t[1]));
			return ab;

		} catch (Exception e) {
			// e.printStackTrace();
		}

		return null;

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object value) {
		if (value != null) {
			return value.getClass() + ":" + String.valueOf(((ModelAb) value).getCodigo());
		} else {
			return null;
		}
	}

}
